//
//  ZQWNetworkTools.swift
//  
//
//  Created by zhuangqianwei on 16/11/14.
//
//

import UIKit
import AFNetworking

enum HTTPMethod: Int {
    case GET = 0
    case POST
}


class ZQWNetworkTools: AFHTTPSessionManager {
//第三方框架的安装AFNeteorking
    
    //声明单例对象
    static let shared: ZQWNetworkTools = {
        let tools = ZQWNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    //声明封装的函数
    
    func request(method: HTTPMethod, UrlSting: String, parameter: Any?,finished:@escaping (Any?,Error?) -> ()) {
        
        let successCallback = { (task: URLSessionDataTask,responseObject: Any?) -> () in
        
        finished(responseObject,nil)
        
    }

        //失败的回调
        let failureCallBack = { (task: URLSessionDataTask?, error: Error) -> () in
            //输出错误信息
            print(error)
            finished(nil, error)
        }
        
        if  method == .GET {
            self.get(UrlSting, parameters: parameter, progress: nil, success: successCallback, failure:                                        failureCallBack)
        }else{
            
            self.post(UrlSting, parameters: parameter, progress: nil, success: successCallback, failure: failureCallBack)
            
            
        }
    }
    
}
